﻿namespace NJQuery {
	using System;
	using System.Web.UI;
	using System.ComponentModel;
	using System.Collections.Generic;
	using System.Globalization;

	/// <summary>
	/// Provides a locked down replacement for the <see cref="ControlCollection"/> object that disallows direct modification of the collection, adding the ability only to add <see cref="GroupControlContainer"/> object to offer multiple grouped child control collections.
	/// </summary>
	public sealed class GroupedControlCollection : ControlCollection, ICollection<Control> {


		#region Constructors

		/// <summary>
		/// Creates a new instance of the <see cref="GroupedControlCollection"/> class.
		/// </summary>
		/// <param name="owner">The <see cref="Control"/> that the collection belongs to.</param>
		public GroupedControlCollection(Control owner) : base(owner) { }

		#endregion


		#region Methods: Internal

		/// <summary>
		/// Adds a <see cref="GroupControlContainer"/> object to the locked down collection.  This is the only modification allowed to the collection.
		/// </summary>
		/// <param name="container"></param>
		public void AddContainer(GroupControlContainer container) {
			base.Add(container);
		}

		#endregion


		#region Methods: Public Overrides

		/// <summary>
		/// Adds the specified <see cref="Control"/> object to the collection.
		/// This method is ignored for the <see cref="GroupedControlCollection"/>.
		/// </summary>
		/// <param name="child">The <See cref="Control"/> to add to the collection.</param>
		[Browsable(false)]
		public override void Add(Control child) { }

		/// <summary>
		/// Adds the specified Control object to the collection at the specified index location.
		/// This method is ignored for the <see cref="GroupedControlCollection"/>.
		/// </summary>
		/// <param name="index">The location in the array at which to add the child control.</param>
		/// <param name="child">The <see cref="Control"/> to add to the collection.</param>
		[Browsable(false)]
		public override void AddAt(int index, Control child) { }

		/// <summary>
		/// Removes all controls from the current server control's ControlCollection object.
		/// This method is ignored for the <see cref="GroupedControlCollection"/>.
		/// </summary>
		[Browsable(false)]
		public override void Clear() { }

		/// <summary>
		/// Removes the specified server control from the parent server control's <see cref="ControlCollection"/> object.
		/// This method is ignored for the <see cref="GroupedControlCollection"/>.
		/// </summary>
		/// <param name="value">The server control to be removed.</param>
		[Browsable(false)]
		public override void Remove(Control value) { }

		/// <summary>
		/// Removes a child control, at the specified index location, from the <see cref="ControlCollection"/> object.
		/// This method is ignored for the <see cref="GroupedControlCollection"/>.
		/// </summary>
		/// <param name="index">The ordinal index of the server control to be removed from the collection.</param>
		[Browsable(false)]
		public override void RemoveAt(int index) { }

		#endregion


		#region Methods: Public

		/// <summary>
		/// Copies the child controls stored in the <see cref="GroupedControlCollection"/> object to <see cref="Control"/> array object, beginning at the specified index location in the array.
		/// </summary>
		/// <param name="array">The <see cref="Control"/> array to copy the child controls to. </param>
		/// <param name="arrayIndex">The zero-based relative index in array where copying begins.</param>
		public void CopyTo(Control[] array, int arrayIndex) {
			base.CopyTo(array, arrayIndex);
		}

		#endregion


		#region Explicit Implementation: ICollection<Control>

		bool ICollection<Control>.IsReadOnly {
			get { return true; }
		}

		bool ICollection<Control>.Remove(Control item) {
			if(Contains(item)) {
				Remove(item);
				return true;
			}
			return false;
		}

		#endregion


		#region Explicit Implementation: IEnumerable<Control>

		IEnumerator<Control> IEnumerable<Control>.GetEnumerator() {
			foreach(Control control in this)
				yield return control;
		}

		#endregion


	}

}